﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;
namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class SelectMainFault : AgentPageBase
    {

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();

            BindGrid1(); 
            // 初始化选中项，放在表格数据绑定之后
            if (_selectedParRowIndex != -1)
            {
                gdPar.SelectedRowIndex = _selectedParRowIndex;
            }

            BindGrid2(obj.ParId == 0 ? -1 : obj.ParId);
            if (_selectedSubRowIndex != -1)
            {
                this.gdSub.SelectedRowIndex = _selectedSubRowIndex;
            }
        }

        // 当前部门ID
        //private string _deptID;
        protected int Id
        {
            get {
                return GetRequestValue<int>("ids",0);
            }
        }
        protected AgentMainFaultInfo _obj = null;
        protected AgentMainFaultInfo obj
        {
            get
            { 
                if(_obj==null)
                {
                    _obj = AgentMainFaultControlller.GetById(Id);
                    _obj = _obj == null ? new AgentMainFaultInfo() : _obj;
                }
                return _obj;
            }
        }
        
        // 用来在表格渲染时记录选中的行索引
        private int _selectedParRowIndex = -1;
        private int _selectedSubRowIndex = -1;
        private void BindGrid1()
        {
            this.gdPar.DataSource = AgentMainFaultControlller.GetAllByParId(0);
            this.gdPar.DataBind();
        }

        private void BindGrid2(int parID)
        {
            DataTable dt = AgentMainFaultControlller.GetAllByParId(parID);
            if (dt.Rows.Count <= 0)
            {
                this.gdSub.DataSource = null;
                this.gdSub.DataBind();
                return;
            }
            // return;
            this.gdSub.DataSource = dt;
            this.gdSub.DataBind();
        }
        #endregion

        #region Events

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            int selectedRowIndex = gdSub.SelectedRowIndex;
            if (selectedRowIndex < 0)
            {
                Alert.Show("请选择一条记录");
                return;
            }

            string deptID = gdSub.DataKeys[selectedRowIndex][0].ToString();
            string deptName = gdSub.DataKeys[selectedRowIndex][1].ToString();

            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(deptID, deptName)
                 + ActiveWindow.GetHideReference());
        }


        #endregion

        protected void gdPar_RowDataBound(object sender, GridRowEventArgs e)
        {
            DataRowView drv = e.DataItem as DataRowView;
            if (drv != null && obj.ParId.ToString() == drv["ID"].ToString())
            {
                _selectedParRowIndex = e.RowIndex;
            }
        }

        protected void gdSub_RowDataBound(object sender, GridRowEventArgs e)
        {
            DataRowView drv = e.DataItem as DataRowView;
            if (drv != null && Id.ToString() == drv["ID"].ToString())
            {
                _selectedSubRowIndex = e.RowIndex;
            }
        }

        protected void gdPar_RowClick(object sender, GridRowClickEventArgs e)
        {
            int parid = Convert.ToInt32(gdPar.DataKeys[e.RowIndex][0]);
            BindGrid2(parid);
        }

        protected void btnClearClose_Click(object sender, EventArgs e)
        {            
            string deptID ="0";
            string deptName = "";

            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(deptID, deptName)
                 + ActiveWindow.GetHideReference());
        }
    }
}